package com.tulele.vtool.service.impl;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.tulele.vtool.mapper.ConnectionInfoMapper;
import com.tulele.vtool.mapper.PageDetailLogMapper;
import com.tulele.vtool.mapper.SysGzhMapper;
import com.tulele.vtool.model.*;
import com.tulele.vtool.model.logEntity.PageDetailLog;
import com.tulele.vtool.model.reqEntity.GetRecommendUserInfoReqBean;
import com.tulele.vtool.service.ConnectionInfoService;
import com.tulele.vtool.service.SysGzhService;
import com.tulele.vtool.utils.DateUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ConnectionInfoServiceImpl implements ConnectionInfoService {

    @Autowired
    private PageDetailLogMapper pageDetailLogMapper;

    @Autowired
    private ConnectionInfoMapper connectionInfoMapper;

    @Override
    public ConnectionPermissionInfo selectPermission(String gzhCode, Integer vipStatus) {
        return this.connectionInfoMapper.selectPermission(gzhCode,vipStatus);
    }

    @Override
    public Integer selectApplyNumByUserId(Integer userId) {
        return this.connectionInfoMapper.selectApplyNumByUserId(userId);
    }

    @Override
    public void sendApply(ConnectionApplyInfo connectionApplyInfo) {
        this.connectionInfoMapper.sendApply(connectionApplyInfo);
    }

    @Override
    public List<ConnectionApplyInfo> selectPassesUserListByUserId(Integer userId, Integer pageNum, Integer pageSize, GetRecommendUserInfoReqBean recommendUserInfoReqBean,String gzhCode,String[] screen) {
        PageHelper.startPage(pageNum,pageSize);
        return this.connectionInfoMapper.selectPassesUserListByUserId(userId,recommendUserInfoReqBean,gzhCode,screen);
    }

    @Override
    public List<ConnectionApplyInfo> selectToUserListByUserId(Integer userId, Integer pageNum, Integer pageSize, GetRecommendUserInfoReqBean recommendUserInfoReqBean,String gzhCode,String[] screen) {
        PageHelper.startPage(pageNum,pageSize);
        return this.connectionInfoMapper.selectToUserListByUserId(userId,recommendUserInfoReqBean,gzhCode,screen);
    }

    @Override
    public Integer selectBeAddedNumByUserId(Integer connectionToUserId) {
        return this.connectionInfoMapper.selectBeAddedNumByUserId(connectionToUserId);
    }

    @Override
    public void deleteUserByConnectionId(Integer connectionId) {
        this.connectionInfoMapper.deleteUserByConnectionId(connectionId);
    }

    @Override
    public Integer selectPassesNumByUserId(Integer userId) {
        return this.connectionInfoMapper.selectPassesNumByUserId(userId);
    }

    @Override
    public void passesUser(Integer id) {
        this.connectionInfoMapper.passesUser(id);
    }

    @Override
    public Integer selectWhetherOrBeAdded(Integer userId) {
        return this.connectionInfoMapper.selectWhetherOrBeAdded(userId);
    }

    @Override
    public void switchState(Integer userId, Integer state) {
        this.connectionInfoMapper.switchState(userId,state);
    }

    @Override
    public void addWhetherOrBeAdded(WxNotifyMsgSetInfo wxNotifyMsgSetInfo) {
        this.connectionInfoMapper.addWhetherOrBeAdded(wxNotifyMsgSetInfo);
    }

    @Override
    public List<ConnectionApplyInfo> selectFromUserListByUserId(Integer userId, Integer pageNum, Integer pageSize, GetRecommendUserInfoReqBean recommendUserInfoReqBean, String gzhCode, String[] screen) {
        PageHelper.startPage(pageNum,pageSize);
        return this.connectionInfoMapper.selectFromUserListByUserId(userId,recommendUserInfoReqBean,gzhCode,screen);
    }

    @Override
    public String selectRegionByUserId(Integer userId) {
        return this.connectionInfoMapper.selectRegionByUserId(userId);
    }

    @Override
    public Integer selectCountToUserListByUserId(Integer userId, String gzhCode) {
        return this.connectionInfoMapper.selectCountToUserListByUserId(userId,gzhCode);
    }

    @Override
    public Integer selectSendApplyId(Integer userId, Integer connectionToUserId, String gzhCode) {
        return this.connectionInfoMapper.selectSendApplyId(userId,connectionToUserId,gzhCode);
    }

    @Override
    public void updateApply(ConnectionApplyInfo connectionApplyInfo) {
        this.connectionInfoMapper.updateApply(connectionApplyInfo);
    }

    @Override
    public Map<String, List<ConnectionNeedInfo>> selectNeedByGzhCode(String gzhCode) {
        Map<String,List<ConnectionNeedInfo>> map = Maps.newHashMap();
        // 查询身份
        List<ConnectionNeedInfo> youAreList = this.connectionInfoMapper.selectYouAreListByGzhCode(gzhCode);
        if (CollectionUtils.isNotEmpty(youAreList)){
            map.put("youAreList",youAreList);
        }
        // 查询目的
        List<ConnectionNeedInfo> wouldYouList = this.connectionInfoMapper.selectWouldYouListListByGzhCode(gzhCode);
        if (CollectionUtils.isNotEmpty(wouldYouList)){
            map.put("wouldYouList",wouldYouList);
        }
        return map;
    }

    @Override
    public void setNeedByUserId(Integer userId, List<Integer> needIds, String gzhCode) {
        // 将该用户全部设置为失效状态
        ConnectionUserNeedInfo connectionUserNeedInfo = new ConnectionUserNeedInfo();
        connectionUserNeedInfo.setGzhCode("normal");
        connectionUserNeedInfo.setUserId(userId);
        connectionUserNeedInfo.setEffectiveStatus(0);
        this.connectionInfoMapper.updateStatus(connectionUserNeedInfo);
        needIds.stream().forEach(needId ->{
            // 查询是否已经设置
                connectionUserNeedInfo.setNeedId(needId);
            Integer id  = this.connectionInfoMapper.selectHaveBeenSet(connectionUserNeedInfo);
            if (null == id){
                connectionUserNeedInfo.setEffectiveStatus(1);
                connectionUserNeedInfo.setCreateTime(DateUtil.getCurDate());
                this.connectionInfoMapper.setNeedByUserId(connectionUserNeedInfo);
            }else {
                // 该条数据已存在 将设置为有效
                connectionUserNeedInfo.setEffectiveStatus(1);
                connectionUserNeedInfo.setId(id);
                this.connectionInfoMapper.updateStatus(connectionUserNeedInfo);
            }

        });
    }

    @Override
    public List<ConnectionNeedInfo> selectNeedByUserId(Integer userId, String gzhCode) {
        return this.connectionInfoMapper.selectNeedByUserId(userId,gzhCode);
    }

    @Override
    public Map<String,String> selectNeedNameByUserId(Integer userId,String gzhCode) {
        Map<String,String> map = Maps.newHashMap();
        // 查询用户身份
        List<String> identityName = this.connectionInfoMapper.selectIdentityNameByUserId(userId,gzhCode);
        if (CollectionUtils.isNotEmpty(identityName)){
            map.put("identityName",String.join(" ",identityName));
        }
        List<String> needName = this.connectionInfoMapper.selectNeedNameByUserId(userId,gzhCode);
        if (CollectionUtils.isNotEmpty(needName)){
            map.put("needName",String.join(" ",needName));
        }
        return map;
    }

    @Override
    public Integer selectNeedBeyond(List<Integer> needIds) {
        return this.connectionInfoMapper.selectNeedBeyond(needIds);
    }

    @Override
    public List<Integer> getUserAttentionArticleTypeInfo(Integer userId) {
        String articleTypeIds = this.connectionInfoMapper.getUserAttentionArticleTypeInfo(userId);
        if (StringUtils.isBlank(articleTypeIds)){
            return null;
        }
        return Arrays.asList(articleTypeIds.split(","))
                .stream()
                .map(Integer::valueOf)
                .collect(Collectors.toList());
    }

    @Override
    public ConnectionApplyInfo selectToUserIdByNeedId(Integer id) {
        return this.connectionInfoMapper.selectToUserIdByNeedId(id);
    }

    @Override
    public void addAccessRecord(PageDetailLog log) {
        this.pageDetailLogMapper.addPageAccess(log);
    }

    @Override
    public Integer notFirstTime(Integer userId) {
        return this.pageDetailLogMapper.notFirstTime(userId);
    }

    @Override
    public Integer isAdd(Integer userId, Integer platformUserId) {
        return this.connectionInfoMapper.isAdd(userId,platformUserId);
    }
}
